﻿using System;
using System.Collections.Generic;
using System.Text;
using Gammar.Price.DataAccess;
using Gammar.Price.ObjectModel;
using Gammar.Price.Utility.Configuration;
using System.Collections.ObjectModel;
using System.Threading;
using System.Globalization;
using Gammar.Price.Utility;
using Longbow.Web.Caching;
using System.ComponentModel;

namespace Gammar.Price.Business
{
    /// <summary>
    /// 
    /// </summary>
    [DataObject]
    public class BusinessObjectLogic
    {
        private const string CacheOpenClassKey = "_$_OpenClass_{0}_{1}_{2}";

        public BusinessObjectLogic()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static DataBlock<ItemEntity> RetrieveList(int pageIndex, int pageCount, string criteria)
        {
            string cacheKey = string.Format(CultureInfo.InvariantCulture, CacheOpenClassKey, pageIndex, pageCount, criteria);
            DataBlock<ItemEntity> ret = CacheManager.Get(cacheKey) as DataBlock<ItemEntity>;
            if (ret == null)
            {
                ViewOpenClassDetail oc = new ViewOpenClassDetail();
                ViewOpenClassDetailEntity[] datas = oc.RetrieveListById(criteria, DatabaseManager.Instance.CurrentDBAccess);
                List<ViewOpenClassDetailEntity> entities = new List<ViewOpenClassDetailEntity>(datas);

                Collection<ItemEntity> items = new Collection<ItemEntity>();
                for (int i = pageIndex * pageCount; i < entities.Count; i++)
                {
                    if (i >= (pageIndex + 1) * pageCount) break;
                    ViewOpenClassDetailEntity entity = entities[i];
                    ItemEntity item = new ItemEntity();
                    item.EntityId = entity.Oc_id.ToString();
                    item.EntityName = StringUtility.Trim(entity.Title, 40);
                    item.CategoryId = entity.Type.ToString();
                    item.CategoryName = entity.Cat_Name;
                    item.PresideUnit = entity.Presideunit;
                    item.SrcWeb = entity.SrcWeb;
                    item.Title = entity.Title;
                    item.Price = entity.Price;
                    items.Add(item);
                }
                ret = new DataBlock<ItemEntity>(items, entities.Count, pageIndex);
                CacheManager.Add(cacheKey, ret);
            }
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string RetrieveItem(string key)
        {
            ViewOpenClassDetailEntity ret = RetrieveItemDetailByKey(key);
            return ret == null ? "" : ret.Content;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static Sp_OpenClassEntity[] RetrieveItemsByOcId(Guid guid)
        {
            Sp_OpenClass sp = new Sp_OpenClass(guid);
            return sp.Execute(DatabaseManager.Instance.CurrentDBAccess);
        }

        public static ViewOpenClassDetailEntity RetrieveItemDetailByKey(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            ViewOpenClassDetail oc = new ViewOpenClassDetail();
            return oc.RetrieveItemById(key, DatabaseManager.Instance.CurrentDBAccess);
        }
    }
}
